﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class RobitWidget : BaseEnergyWidget
    {
        public ComponentRobit Component;
        //0-10 工作台
        //11-14 熔炉
        //15-17 配件
        //18-33 背包
        public ImageWidget Home = new ImageWidget() { Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Button/home"), Size = new Vector2(54) };
        public ImageWidget Inventory = new ImageWidget() { Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Button/inventory"), Size = new Vector2(54) };
        public ImageWidget Craft = new ImageWidget() { Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Button/crafting"), Size = new Vector2(54) };
        public ImageWidget Furnace = new ImageWidget() { Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Button/smelting"), Size = new Vector2(54) };
        public ImageWidget Config = new ImageWidget() { Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Button/stock_control"), Size = new Vector2(54) };
        public MekProgressWidget FurCraftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.down) { Size = new Vector2(32, 50), VerticalAlignment = WidgetAlignment.Center };

        public ListPanelWidget FuncList = new ListPanelWidget() { Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Center };
        public CanvasWidget DrawWidget = new CanvasWidget() { Size = new Vector2(240, 320), HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
        public CanvasWidget FuncWidget = new CanvasWidget() { Size = new Vector2(48, 260), VerticalAlignment = WidgetAlignment.Center };
        public RobitWidget(ComponentRobit robit, ComponentMiner miner) : base(miner)
        {
            Component = robit;

            FuncList.ItemWidgetFactory = (obj) =>
            {
                return (ImageWidget)obj;
            };
            FuncList.AddItem(Home);
            FuncList.AddItem(Inventory);
            FuncList.AddItem(Craft);
            FuncList.AddItem(Furnace);
            FuncList.AddItem(Config);
            CanvasMain.Children.Add(DrawWidget);
            FuncWidget.Children.Add(FuncList);
            CanvasMain.Children.Add(FuncWidget);
            InitInventory();

        }
        public void InitInventory()
        {
            DrawWidget.Children.Clear();
            int num = 18;
            GridPanelWidget gridPanel = new GridPanelWidget() { HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            gridPanel.ColumnsCount = 4;
            gridPanel.RowsCount = 4;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    InventorySlotWidget slotWidget = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(4, 2), BevelColor = Color.Gray };
                    slotWidget.AssignInventorySlot(Component, num++);
                    gridPanel.Children.Add(slotWidget);
                    gridPanel.SetWidgetCell(slotWidget, new Point2(j, i));
                }
            }
            DrawWidget.Children.Add(gridPanel);

            setTitle("机器人-背包");
        }
        public void InitFurnace()
        {
            DrawWidget.Children.Clear();
            InventorySlotWidget inventory = new InventorySlotWidget() { Size = new Vector2(48), BevelColor = Color.Black };
            inventory.AssignInventorySlot(Component, 11);//物品位1
            InventorySlotWidget inventory1 = new InventorySlotWidget() { Size = new Vector2(48), BevelColor = Color.Black };
            inventory1.AssignInventorySlot(Component, 12);//物品位2
            InventorySlotWidget inventory3 = new InventorySlotWidget() { Size = new Vector2(48), BevelColor = Color.Yellow, VerticalAlignment = WidgetAlignment.Far };
            inventory3.AssignInventorySlot(Component, 13);//保留位
            InventorySlotWidget inventory4 = new InventorySlotWidget() { Size = new Vector2(48), VerticalAlignment = WidgetAlignment.Far };
            inventory4.AssignInventorySlot(Component, 14);//结果位
            inventory.Margin = new Vector2(30, 30);
            inventory1.Margin = new Vector2(90, 30);
            inventory3.Margin = new Vector2(30, 30);
            inventory4.Margin = new Vector2(90, 30);
            FurCraftProgress.Margin = new Vector2(60, 0);
            DrawWidget.Children.Add(inventory);
            DrawWidget.Children.Add(inventory1);
            DrawWidget.Children.Add(inventory3);
            DrawWidget.Children.Add(inventory4);
            DrawWidget.Children.Add(FurCraftProgress);
            setTitle("机器人-熔炼");
        }
        public void InitConfig()
        {
            DrawWidget.Children.Clear();

            LabelWidget label = new LabelWidget() { Text = "配置升级", FontScale = 0.7f, Color = Color.Green, Margin = new Vector2(10, 10) };
            InventorySlotWidget slotWidget = new InventorySlotWidget() { Size = new Vector2(32), Margin = new Vector2(20, 40), BevelColor = Color.Gray };
            slotWidget.AssignInventorySlot(Component, 15);
            InventorySlotWidget slotWidget2 = new InventorySlotWidget() { Size = new Vector2(32), Margin = new Vector2(58, 40), BevelColor = Color.Gray };
            slotWidget2.AssignInventorySlot(Component, 16);
            InventorySlotWidget slotWidget3 = new InventorySlotWidget() { Size = new Vector2(32), Margin = new Vector2(93, 40), BevelColor = Color.Gray };
            slotWidget3.AssignInventorySlot(Component, 17);

            LabelWidget label2 = new LabelWidget() { Text = "功能设置", FontScale = 0.7f, Color = Color.Green, Margin = new Vector2(10, 80) };


            CheckboxWidget IsFollow = new CheckboxWidget("是否跟随玩家") { Margin = new Vector2(20, 110), VerticalAlignment = WidgetAlignment.Far };
            CheckboxWidget IsCharge = new CheckboxWidget("充电模式") { Margin = new Vector2(20, 60), VerticalAlignment = WidgetAlignment.Far };
            CheckboxWidget IsGetDrop = new CheckboxWidget("主动拾取掉落物") { Margin = new Vector2(20, 10), VerticalAlignment = WidgetAlignment.Far };

            IsFollow.IsCheck = Component.IsFollow;
            IsCharge.IsCheck = Component.IsCharge;
            IsGetDrop.IsCheck = Component.IsGetDrop;
            IsFollow.CheckChange = () => { Component.IsFollow = IsFollow.IsCheck; };
            IsCharge.CheckChange = () => { Component.IsCharge = IsCharge.IsCheck; };
            IsGetDrop.CheckChange = () => { Component.IsGetDrop = IsGetDrop.IsCheck; };
            DrawWidget.Children.Add(label);
            DrawWidget.Children.Add(slotWidget);
            DrawWidget.Children.Add(slotWidget2);
            DrawWidget.Children.Add(slotWidget3);
            DrawWidget.Children.Add(label2);
            DrawWidget.Children.Add(IsFollow);
            DrawWidget.Children.Add(IsCharge);
            DrawWidget.Children.Add(IsGetDrop);
            setTitle("机器人-配置");
        }
        public void InitCraft()
        {
            DrawWidget.Children.Clear();
            MekProgressWidget craftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.installing_right) { Size = new Vector2(32, 32) };
            StackPanelWidget horizontal = new StackPanelWidget() { Direction = LayoutDirection.Horizontal };
            StackPanelWidget vertical = new StackPanelWidget() { Direction = LayoutDirection.Vertical, Margin = new Vector2(10) };
            GridPanelWidget gridPanelWidget = new GridPanelWidget() { ColumnsCount = 3, RowsCount = 3, Margin = new Vector2(10) };
            craftProgress.SetProgress(1f);
            int num = 0;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget() { Size = new Vector2(48) };
                    inventorySlotWidget.Margin = new Vector2(2f);
                    inventorySlotWidget.AssignInventorySlot(Component, num++);
                    gridPanelWidget.Children.Add(inventorySlotWidget);
                    gridPanelWidget.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }
            horizontal.Children.Add(craftProgress);
            InventorySlotWidget RemainSlot = new InventorySlotWidget() { Size = new Vector2(48), Margin = new Vector2(10, 0)};
            RemainSlot.AssignInventorySlot(Component, 9);
            RemainSlot.Children.Add(new LabelWidget() { Text = "...", HorizontalAlignment = WidgetAlignment.Center, Color = new Color(0, 0, 0, 96) });
            horizontal.Children.Add(RemainSlot);
            InventorySlotWidget ResultSlot = new InventorySlotWidget() { Size = new Vector2(48) };
            ResultSlot.AssignInventorySlot(Component, 10);
            horizontal.Children.Add(ResultSlot);
            vertical.Children.Add(gridPanelWidget);
            vertical.Children.Add(horizontal);
            DrawWidget.Children.Add(vertical);
            setTitle("机器人-工作台");
        }

        public override void Update()
        {
            if (Input.Click.HasValue)
            {
                Segment2 segment = Input.Click.Value;
                if (Home.HitTest(segment.Start) && Home.HitTest(segment.End))
                {
                    DialogsManager.ShowDialog(null, new MessageDialog("消息", "是否将机器人传回出生点", "确定", "取消", (obj) =>
                    {
                        if (obj == MessageDialogButton.Button1)
                        {
                            Component.componentBody.Position = new Vector3(Component.HomePoint);
                            Component.IsFollow = false;
                        }
                        DialogsManager.HideAllDialogs();
                    }));
                }
                else if (Inventory.HitTest(segment.Start) && Inventory.HitTest(segment.End))
                {
                    InitInventory();

                }
                else if (Furnace.HitTest(segment.Start) && Furnace.HitTest(segment.End))
                {
                    InitFurnace();
                }
                else if (Craft.HitTest(segment.Start) && Craft.HitTest(segment.End))
                {
                    InitCraft();
                }
                else if (Config.HitTest(segment.Start) && Config.HitTest(segment.End))
                {
                    InitConfig();
                }
            }
            SetPowerProgress((float)Component.Power / (float)Component.MaxPower);
            FurCraftProgress.SetProgress(Component.CraftProgress);
            SetLeftBottomMoreInfo($"消耗:{ILibrary.FormatPower(Component.OutputPowerLimit)}\n储能:{ILibrary.FormatPower(Component.Power)}\n最大:{ILibrary.FormatPower(Component.MaxPower)}");
        }

    }
}
